/*******************************************************************************
* KindEditor - WYSIWYG HTML Editor for Internet
* Copyright (C) 2006-2011 kindsoft.net
*
* @author Roddy <luolonghao@gmail.com>
* @site http://www.kindsoft.net/
* @licence http://www.kindsoft.net/license.php
*******************************************************************************/


KindEditor.plugin('custom', function(K) {
	var self = this, name = 'custom';
	self.clickToolbar(name, function() {
		self.focus();
//		var html = self.html();
//		self.insertHtml('<div class="item_container"><img class="item_img" src="http://img.xmiles.cn/worthbuying/2015_10/20151026171551_948_144585095194811.tmp"/>'+
//				'<div class="item_content"><div class="item_source"></div><div class="item_salenum"></div><div class="clear"></div>'+
//				'<div class="item_title"></div><div class="item_postage"></div><div class="item_discount_container"><div class="item_discount_arrow"></div><div class="item_discount"></div></div>'+
//				'<div class="clear"></div><div class="item_price"></div><div class="item_oldprice"></div></div></div>');
		editTopicProduct(self);
	});
	
});


function editTopicProduct(self) {
	
	var url = "/worthbuying/singleProductList.action";
	url = encodeURI(url);
	openDialogWithRand(url,1400,1000,"商品列表",function (data){
		updateProductList(self, data);
	});
}

function updateProductList(self, data) {
	if (typeof(data) == "undefined") {
		return;
	}
	/* $("#productList tbody tr.dataRow").each(function(index, item) {
		$(item).remove();
	}); */
	
	var arr = Object.keys(data);
	for (var i = 0;i < arr.length;i++) {
		var info = data[arr[i]];
		addProduct(self, info);
	}
	
}

function addProduct(self, info) {
	
	var source;
	if (info["sourceType"] == 10) {
		source= "天猫";
	} else if (info["sourceType"] == 7) {
		source= "京东";
	} else if (info["sourceType"] == 8) {
		source= "淘宝";
	} else if (info["sourceType"] == 2) {
		source= "大众点评";
	} else if (info["sourceType"] == 11) {
		source= "苏宁易购";
	} else if (info["sourceType"] == 12) {
		source= "易迅网";
	} else if (info["sourceType"] == 13) {
		source= "1号店";
	} else if (info["sourceType"] == 14) {
		source= "国美在线";
	}
	var obj = '{"url":"'+info["buyurl"]+'", "action":{"launch":"launch_vc_worth_outlink_detail","launchParams":{"id":-23'+info["sourceId"]+',"sourcetype":'+info["sourceType"]+',"htmlUrl":"'+info["buyurl"]+'","title":"'+source+'"}}}';
//	var url = encodeURI(info["buyurl"]);
//	var action ='{"launch":"launch_vc_worth_outlink_detail","launchParams":{"id":'+info["sourceId"]+',"sourcetype":'+info["sourceType"]+',"htmlUrl":"baidu.com","title":"'+source+'商品"}}';
	var html = '<br><div class="item_container" id="'+escape(obj)+'"><img class="item_img" src="'+info["img"] + '" />'+
	'<div class="item_content">'+
	'<div class="item_source">'+source+'</div>'+
	'<div class="item_salenum">已售'+info["salednum"] + '</div><div class="clear"></div>'+
	'<div class="item_title">'+info["title"]+'</div>';
	if (info["isfreeshipping"] == 1) {
		html +='<div class="item_postage">包邮</div>';
	}
	html += '<div class="clear"></div><div class="item_price">¥'+info["price"] +'</div>';
	if (parseInt(info["oprice"]) > 0) {
		html += '<div class="item_oldprice">¥'+info["oprice"] + '</div>';
	}
	html += '<div class="item_click_tips">查看详情</div>';
	html += '</div></div><br>';
	self.insertHtml(html);
	
//	var html = [
//				'<div class="item_container"><img class="item_img" src="'+info["img"] + '" />',
//				'<div class="item_content"><div class="item_source"></div><div class="item_salenum"></div><div class="clear"></div>',
//				'<div class="item_title">'+info["title"]+'</div><div class="item_postage"></div><div class="item_discount_container"><div class="item_discount_arrow"></div><div class="item_discount"></div></div>',
//				'<div class="clear"></div><div class="item_price"></div><div class="item_oldprice"></div></div></div><br>'
//				];
//	self.insertHtml(html);
    
}
